﻿#region Using Directives

    using System;
    using System.Linq;
    using System.Text;

    using Csla;
    using Csla.Data;
    using Csla.Server;
    using Csla.Reflection;

    using Common = MarketPulse.BusinessLogic.Common;
    using MarketPulse.BusinessLogic.Common.Criteria;

    using Private = MarketPulse.BusinessLogic.Private;

    using MarketPulse.DataAccess.DataContextObjects.Production;

#endregion

namespace MarketPulse.DataAccess.Factory
{
    class AccreditationFactory : ObjectFactory
    {

        #region Member Variables

            /// <summary>
            /// Property that stores the name of the class.
            /// </summary>
            private static readonly Type mClass = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;

            /// <summary>
            /// Property that stores the log4net logger for this class.
            /// </summary>
            private static readonly log4net.ILog mLog = log4net.LogManager.GetLogger(mClass);

        #endregion

        #region Methods

            #region Create Methods

                ///// <summary>
                ///// Method used to create and return a new Accreditation object instance
                ///// </summary>
                ///// <returns>The newly created Accreditation object</returns>
                //public Private.Accreditation Create()
                //{
                //    // Create a new instance of the object
                //    var accreditation = (Private.Accreditation)MethodCaller.CreateInstance(typeof(Private.Accreditation));

                //    accreditation.Obsolete = false;

                //    // Mark it as a new object
                //    MarkNew(accreditation);
                //    CheckRules(accreditation);

                //    // Mark the Accreditation as a child element
                //    MarkAsChild(accreditation);

                //    return accreditation;
                //}

                ///// <summary>
                ///// Method used to create a new instance an AccreditationList object populated with any default values
                ///// </summary>
                ///// <returns>The newly instantiated AccreditationList list object</returns>
                //public Private.AccreditationList CreateList()
                //{
                //    // Create a new instance of the object
                //    var accreditationList = (Private.AccreditationList)MethodCaller.CreateInstance(typeof(Private.AccreditationList));

                //    // Mark it as a new object
                //    MarkNew(accreditationList);

                //    MarkAsChild(accreditationList);

                //    return accreditationList;
                //}

                /// <summary>
                /// Method used to create and return a new UserAccreditation object instance
                /// </summary>
                /// <returns>The newly created UserAccreditation object</returns>
                public Common.UserAccreditation CreateUserAccreditation()
                {
                    // Create a new instance of the object
                    var userAccreditation = (Common.UserAccreditation)MethodCaller.CreateInstance(typeof(Common.UserAccreditation));

                    // Mark it as a new object
                    MarkNew(userAccreditation);
                    CheckRules(userAccreditation);

                    MarkAsChild(userAccreditation);

                    return userAccreditation;
                }

            #endregion

            #region Fetch Methods

                #region AccreditationInfo

                    ///// <summary>
                    ///// Method used to fetch a Read Only copy of an existing Accreditation from the database given a guid and version id.
                    ///// </summary>
                    ///// <param name="criteria">The criteria object holding the Guid and versionID of the Accreditation to retrieve</param>
                    ///// <returns>The Accreditation object retrieved from the database</returns>
                    //public BusinessLogic.Common.AccreditationInfo FetchInfo(SingleCriteria<Guid> criteria)
                    //{
                    //    try
                    //    {
                    //        using (var mgr = ContextManager<MUM_dev>.GetManager(Database.BusinessSystem))
                    //        {
                    //            // Create an instance to return
                    //            var accreditationInfo = (BusinessLogic.Common.AccreditationInfo)MethodCaller.CreateInstance(typeof(BusinessLogic.Common.AccreditationInfo));

                    //            // Fetch the data from the database based on the criteria
                    //            var selectResult = mgr.DataContext.UspAccreditationSelect(criteria.Value, null);

                    //            // Get the result
                    //            UspAccreditationSelectResult result = null;
                    //            try
                    //            {
                    //                result = selectResult.Single();
                    //            }
                    //            catch (InvalidOperationException ioe)
                    //            {
                    //                // Construct error message
                    //                StringBuilder errorMessage = new StringBuilder();
                    //                errorMessage.Append("Unable to locate exactly one Accreditation with Guid = [ ");
                    //                errorMessage.Append(criteria.Value);
                    //                errorMessage.Append(" ]");
                    //                mLog.Error(errorMessage.ToString(), ioe);
                    //                throw new DataAccessException(errorMessage.ToString());
                    //            }

                    //            // Common properties inherited from MarketPulseBase<Branch>
                    //            LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.GUIDProperty, criteria.Value);
                    //            LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.ObsoleteProperty, result.Obsolete.Value);
                    //            LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.InsertDateTimeProperty, result.InsertDateTime.Value);
                    //            LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.InsertUserGUIDProperty, result.InsertUserGUID.Value);
                    //            LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.UpdateDateTimeProperty, result.UpdateDateTime.Value);
                    //            LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.UpdateUserGUIDProperty, result.UpdateUserGUID.Value);
                    //            // Other properties
                                
                    //            LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.CompanyGUIDProperty, result.CompanyGUID.Value);
                    //            LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.ComponentGUIDProperty, result.ComponentGUID.Value);
                    //            LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.AccreditationTitleProperty, result.AccreditationTitle);
                    //            LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.AccreditationXMLProperty, result.AccreditationXML.ToString());
                    //            //LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.PassPercentageProperty, result.PassPercentage.Value);
                    //            LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.MaxAttemptsProperty, result.MaxAttempts.Value);
                    //            LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.ShowSummaryProperty, result.ShowSummary.Value);
                    //            LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.AccreditationDescriptionProperty, result.AccreditationDescription);
                    //            LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.EffectiveStartDateTimeProperty, result.EffectiveStartDateTime);
                    //            LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.EffectiveEndDateTimeProperty, result.EffectiveEndDateTime);

                    //            // Set the Accreditation version ID if it has a value
                    //            if (result.AccreditationVersionID.HasValue)
                    //            {
                    //                LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.VersionIDProperty, result.AccreditationVersionID.Value);
                    //            }
                              
                    //            // Make sure that CSLA knows this has changed since being instantiated
                    //            MarkOld(accreditationInfo);                               

                    //            return accreditationInfo;
                    //        }
                    //    }
                    //    catch (DataAccessException dae)
                    //    {
                    //        mLog.Error(dae.ToString());
                    //        throw dae;
                    //    }
                    //    catch (Exception ex)
                    //    {
                    //        mLog.Error(ex.ToString());
                    //        throw ex;
                    //    }
                    //}

                #endregion

                #region AccreditationInfoList

                    /// <summary>
                    /// Method used to fetch a read only list of read only Accreditation objects from the database
                    /// </summary>                    
                    /// <returns>A read only list of read only Accreditation objects retrieved from the database</returns>
                    public Common.AccreditationInfoList FetchInfoList()
                    {
                        try
                        {
                            using (var manager = ContextManager<MUM_dev>.GetManager(Database.BusinessSystem))
                            {
                                // Create an instance to return
                                var accreditationInfoList = (Common.AccreditationInfoList)MethodCaller.CreateInstance(typeof(Common.AccreditationInfoList));

                                // Fetch the data from the database based on the criteria
                                var selectResult = manager.DataContext.UspAccreditationSelectAll(false);

                                // AccreditationInfoList is a read-only collection,
                                // so typically, when the data access is implemented in
                                // DataPortal_Fetch within the business object, the
                                // private IsReadOnly property must be set false in order to 
                                // populate the collection.  Here, within a factory 
                                // , it isn't visible so one must resort to calling the 
                                // SetIsReadOnly method provided by our parent, ObjectFactory.
                                SetIsReadOnly(accreditationInfoList, false);
                                foreach (UspAccreditationSelectAllResult result in selectResult)
                                {
                                    Common.AccreditationInfo accreditationInfo = (Common.AccreditationInfo)MethodCaller.CreateInstance(typeof(Common.AccreditationInfo));

                                    // Common properties inherited from MarketPulseBase<Branch>
                                    LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.GUIDProperty, result.AccreditationGUID);
                                    LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.ObsoleteProperty, result.Obsolete.Value);
                                    LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.InsertDateTimeProperty, result.InsertDateTime.Value);
                                    LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.InsertUserGUIDProperty, result.InsertUserGUID.Value);
                                    LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.UpdateDateTimeProperty, result.UpdateDateTime.Value);
                                    LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.UpdateUserGUIDProperty, result.UpdateUserGUID.Value);
                                    // Other properties

                                    LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.CompanyGUIDProperty, result.CompanyGUID.Value);
                                    
                                    LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.AccreditationTitleProperty, result.AccreditationTitle);
                                    LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.AccreditationXMLProperty, result.AccreditationXML.ToString());
                                    LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.PassPercentageProperty, result.PassPercentage.Value);
                                    LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.MaxAttemptsProperty, result.MaxAttempts.Value);
                                    LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.ShowSummaryProperty, result.ShowSummary.Value);
                                    LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.AccreditationDescriptionProperty, result.AccreditationDescription);

                                    // Set the ComponentGUID if it has a value
                                    if (result.ComponentGUID.HasValue)
                                    {
                                        LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.ComponentGUIDProperty, result.ComponentGUID.Value);
                                    }

                                    // Set the Accreditation version ID if it has a value
                                    if (result.AccreditationVersionID.HasValue)
                                    {
                                        LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.VersionIDProperty, result.AccreditationVersionID.Value);
                                    }

                                    // Set the EffectiveStartDateTime if it has a value
                                    if (result.EffectiveStartDateTime.HasValue)
                                    {
                                        LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.EffectiveStartDateTimeProperty, result.EffectiveStartDateTime);
                                    }

                                    // Set the EffectiveEndDateTime if it has a value
                                    if (result.EffectiveEndDateTime.HasValue)
                                    {
                                        LoadProperty(accreditationInfo, BusinessLogic.Common.AccreditationInfo.EffectiveEndDateTimeProperty, result.EffectiveEndDateTime);
                                    }

                                    // Make sure that CSLA knows this has changed since being instantiated
                                    MarkOld(accreditationInfo);

                                    // Add the item to the list
                                    accreditationInfoList.Add(accreditationInfo);
                                }
                                SetIsReadOnly(accreditationInfoList, true);

                                // Make sure that CSLA knows this has changed since being instantiated
                                MarkOld(accreditationInfoList);

                                return accreditationInfoList;
                            }
                        }
                        catch (InvalidOperationException ioe)
                        {
                            mLog.Error(ioe.ToString());
                            throw ioe;
                        }
                        catch (Exception ex)
                        {
                            mLog.Error(ex.ToString());
                            throw ex;
                        }
                    }

                #endregion

                #region Accreditation

                    /// <summary>
                    /// Method used to fetch an existing Accreditation from the database given a guid and version id.
                    /// </summary>
                    /// <param name="criteria">The criteria object holding the Guid and versionID of the Accreditation to retrieve</param>
                    /// <returns>The Accreditation object retrieved from the database</returns>
                    public Private.Accreditation Fetch(SingleCriteria<Guid> criteria)
                    {
                        try
                        {
                            using (var mgr = ContextManager<MUM_dev>.GetManager(Database.BusinessSystem))
                            {
                                // Create an instance to return
                                var accreditation = (Private.Accreditation)MethodCaller.CreateInstance(typeof(Private.Accreditation));

                                // Fetch the data from the database based on the criteria
                                var selectResult = mgr.DataContext.UspAccreditationSelect(criteria.Value, null);

                                // Get the result
                                UspAccreditationSelectResult result = null;
                                try
                                {
                                    result = selectResult.Single();
                                }
                                catch (InvalidOperationException ioe)
                                {
                                    // Construct error message
                                    StringBuilder errorMessage = new StringBuilder();
                                    errorMessage.Append("Unable to locate exactly one Accreditation with Guid = [ ");
                                    errorMessage.Append(criteria.Value);
                                    errorMessage.Append(" ]");
                                    mLog.Error(errorMessage.ToString(), ioe);
                                    throw new DataAccessException(errorMessage.ToString());
                                }

                                // Common properties inherited from MarketPulseBase<Branch>
                                LoadProperty(accreditation, Private.Accreditation.GUIDProperty, result.AccreditationGUID);
                                LoadProperty(accreditation, Private.Accreditation.ObsoleteProperty, result.Obsolete.Value);
                                LoadProperty(accreditation, Private.Accreditation.InsertDateTimeProperty, result.InsertDateTime.Value);
                                LoadProperty(accreditation, Private.Accreditation.InsertUserGUIDProperty, result.InsertUserGUID.Value);
                                LoadProperty(accreditation, Private.Accreditation.UpdateDateTimeProperty, result.UpdateDateTime.Value);
                                LoadProperty(accreditation, Private.Accreditation.UpdateUserGUIDProperty, result.UpdateUserGUID.Value);
                                // Other properties

                                LoadProperty(accreditation, BusinessLogic.Private.Accreditation.CompanyGUIDProperty, result.CompanyGUID.Value);

                                LoadProperty(accreditation, Private.Accreditation.AccreditationTitleProperty, result.AccreditationTitle);
                                LoadProperty(accreditation, Private.Accreditation.AccreditationXMLProperty, result.AccreditationXML.ToString());
                                LoadProperty(accreditation, Private.Accreditation.PassPercentageProperty, result.PassPercentage.Value);
                                LoadProperty(accreditation, Private.Accreditation.MaxAttemptsProperty, result.MaxAttempts.Value);
                                LoadProperty(accreditation, Private.Accreditation.ShowSummaryProperty, result.ShowSummary.Value);
                                LoadProperty(accreditation, Private.Accreditation.AccreditationDescriptionProperty, result.AccreditationDescription);

                                // Set the ComponentGUID if it has a value
                                if (result.ComponentGUID.HasValue)
                                {
                                    LoadProperty(accreditation, BusinessLogic.Private.Accreditation.ComponentGUIDProperty, result.ComponentGUID.Value);
                                }

                                // Set the Accreditation version ID if it has a value
                                if (result.AccreditationVersionID.HasValue)
                                {
                                    LoadProperty(accreditation, BusinessLogic.Private.Accreditation.VersionIDProperty, result.AccreditationVersionID.Value);
                                }

                                // Set the EffectiveStartDateTime if it has a value
                                if (result.EffectiveStartDateTime.HasValue)
                                {
                                    LoadProperty(accreditation, BusinessLogic.Private.Accreditation.EffectiveStartDateTimeProperty, result.EffectiveStartDateTime);
                                }

                                // Set the EffectiveEndDateTime if it has a value
                                if (result.EffectiveEndDateTime.HasValue)
                                {
                                    LoadProperty(accreditation, BusinessLogic.Private.Accreditation.EffectiveEndDateTimeProperty, result.EffectiveEndDateTime);
                                }

                                // Make sure that CSLA knows this has changed since being instantiated
                                MarkOld(accreditation);

                                return accreditation;
                            }
                        }
                        catch (DataAccessException dae)
                        {
                            mLog.Error(dae.ToString());
                            throw dae;
                        }
                        catch (Exception ex)
                        {
                            mLog.Error(ex.ToString());
                            throw ex;
                        }
                    }

                #endregion

                #region AccreditationList

                    /// <summary>
                    /// Method used to fetch a list of Accreditation objects from the database
                    /// </summary>                    
                    /// <returns>A read only list of  Accreditation objects retrieved from the database</returns>
                    public Private.AccreditationList FetchList()
                    {
                        try
                        {
                            using (var manager = ContextManager<MUM_dev>.GetManager(Database.BusinessSystem))
                            {
                                // Create an instance to return
                                var accreditationList = (Private.AccreditationList)MethodCaller.CreateInstance(typeof(Private.AccreditationList));

                                // Fetch the data from the database based on the criteria
                                var selectResult = manager.DataContext.UspAccreditationSelectAll(false);
                                                            
                                foreach (UspAccreditationSelectAllResult result in selectResult)
                                {
                                    Private.Accreditation accreditation = (Private.Accreditation)MethodCaller.CreateInstance(typeof(Private.Accreditation));

                                    // Common properties inherited from MarketPulseBase<Branch>
                                    LoadProperty(accreditation, Private.Accreditation.GUIDProperty, result.AccreditationGUID);
                                    LoadProperty(accreditation, Private.Accreditation.ObsoleteProperty, result.Obsolete.Value);
                                    LoadProperty(accreditation, Private.Accreditation.InsertDateTimeProperty, result.InsertDateTime.Value);
                                    LoadProperty(accreditation, Private.Accreditation.InsertUserGUIDProperty, result.InsertUserGUID.Value);
                                    LoadProperty(accreditation, Private.Accreditation.UpdateDateTimeProperty, result.UpdateDateTime.Value);
                                    LoadProperty(accreditation, Private.Accreditation.UpdateUserGUIDProperty, result.UpdateUserGUID.Value);
                                    // Other properties

                                    LoadProperty(accreditation, BusinessLogic.Private.Accreditation.CompanyGUIDProperty, result.CompanyGUID.Value);

                                    LoadProperty(accreditation, Private.Accreditation.AccreditationTitleProperty, result.AccreditationTitle);
                                    LoadProperty(accreditation, Private.Accreditation.AccreditationXMLProperty, result.AccreditationXML.ToString());
                                    LoadProperty(accreditation, Private.Accreditation.PassPercentageProperty, result.PassPercentage.Value);
                                    LoadProperty(accreditation, Private.Accreditation.MaxAttemptsProperty, result.MaxAttempts.Value);
                                    LoadProperty(accreditation, Private.Accreditation.ShowSummaryProperty, result.ShowSummary.Value);
                                    LoadProperty(accreditation, Private.Accreditation.AccreditationDescriptionProperty, result.AccreditationDescription);

                                    // Set the ComponentGUID if it has a value
                                    if (result.ComponentGUID.HasValue)
                                    {
                                        LoadProperty(accreditation, BusinessLogic.Private.Accreditation.ComponentGUIDProperty, result.ComponentGUID.Value);
                                    }

                                    // Set the Accreditation version ID if it has a value
                                    if (result.AccreditationVersionID.HasValue)
                                    {
                                        LoadProperty(accreditation, BusinessLogic.Private.Accreditation.VersionIDProperty, result.AccreditationVersionID.Value);
                                    }

                                    // Set the EffectiveStartDateTime if it has a value
                                    if (result.EffectiveStartDateTime.HasValue)
                                    {
                                        LoadProperty(accreditation, BusinessLogic.Private.Accreditation.EffectiveStartDateTimeProperty, result.EffectiveStartDateTime);
                                    }

                                    // Set the EffectiveEndDateTime if it has a value
                                    if (result.EffectiveEndDateTime.HasValue)
                                    {
                                        LoadProperty(accreditation, BusinessLogic.Private.Accreditation.EffectiveEndDateTimeProperty, result.EffectiveEndDateTime);
                                    }

                                    // Mark the Accreditation as a child element
                                    MarkAsChild(accreditation);

                                    // Make sure that CSLA knows this has changed since being instantiated
                                    MarkOld(accreditation);

                                    // Add the item to the list
                                    accreditationList.Add(accreditation);
                                }                               

                                // Make sure that CSLA knows this has changed since being instantiated
                                MarkOld(accreditationList);

                                return accreditationList;
                            }
                        }
                        catch (InvalidOperationException ioe)
                        {
                            mLog.Error(ioe.ToString());
                            throw ioe;
                        }
                        catch (Exception ex)
                        {
                            mLog.Error(ex.ToString());
                            throw ex;
                        }
                    }

                #endregion

                #region UserAccreditationList

                    /// <summary>
                    /// Method used to fetch a list of Accreditation objects from the database
                    /// </summary>
                    /// <param name="criteria">The criteria object holding the Guid and versionID of the Accreditation to retrieve</param>
                    /// <returns>A read only list of  Accreditation objects retrieved from the database</returns>
                    public Common.UserAccreditationList FetchUserAccreditationList(SingleCriteria<Guid> criteria)
                    {
                        try
                        {
                            using (var manager = ContextManager<MUM_dev>.GetManager(Database.BusinessSystem))
                            {
                                // Create an instance to return
                                var userAccreditationList = (Common.UserAccreditationList)MethodCaller.CreateInstance(typeof(Common.UserAccreditationList));

                                // Fetch the data from the database based on the criteria
                                var selectResult = manager.DataContext.UspUserAccreditationSelectByAccreditationGUID(criteria.Value);

                                foreach (UspUserAccreditationSelectByAccreditationGUIDResult result in selectResult)
                                {
                                    Common.UserAccreditation userAccreditation = (Common.UserAccreditation)MethodCaller.CreateInstance(typeof(Common.UserAccreditation));

                                    // Common properties inherited from MarketPulseBase<Branch>
                                    LoadProperty(userAccreditation, Common.UserAccreditation.GUIDProperty, result.UserAccreditationGUID.Value);
                                    //LoadProperty(userAccreditation, Common.UserAccreditation.ObsoleteProperty, result.Obsolete.Value);
                                    LoadProperty(userAccreditation, Common.UserAccreditation.InsertDateTimeProperty, result.InsertDateTime.Value);
                                    //LoadProperty(userAccreditation, Common.UserAccreditation.InsertUserGUIDProperty, result.InsertUserGUID.Value);
                                    //LoadProperty(userAccreditation, Common.UserAccreditation.UpdateDateTimeProperty, result.UpdateDateTime.Value);
                                    //LoadProperty(userAccreditation, Common.UserAccreditation.UpdateUserGUIDProperty, result.UpdateUserGUID.Value);                                                                 
                                    
                                    // Other properties
                                    LoadProperty(userAccreditation, BusinessLogic.Common.UserAccreditation.AccreditationGUIDProperty, criteria.Value);
                                    LoadProperty(userAccreditation, Common.UserAccreditation.UserGUIDProperty, result.UserGUID.Value);
                                    LoadProperty(userAccreditation, Common.UserAccreditation.VersionIDProperty, result.AccreditationVersionID.Value);
                                    LoadProperty(userAccreditation, Common.UserAccreditation.AccreditedProperty, result.Accredited.Value);                                   

                                    // Mark the Accreditation as a child element
                                    MarkAsChild(userAccreditation);

                                    // Make sure that CSLA knows this has changed since being instantiated
                                    MarkOld(userAccreditation);

                                    // Add the item to the list
                                    userAccreditationList.Add(userAccreditation);
                                }

                                // Make sure that CSLA knows this has changed since being instantiated
                                MarkOld(userAccreditationList);

                                return userAccreditationList;
                            }
                        }
                        catch (InvalidOperationException ioe)
                        {
                            mLog.Error(ioe.ToString());
                            throw ioe;
                        }
                        catch (Exception ex)
                        {
                            mLog.Error(ex.ToString());
                            throw ex;
                        }
                    }

                #endregion

            #endregion

            #region Update Methods

                    /// <summary>
                    /// Method used to persist an altered Accreditation object in the database.
                    /// </summary>
                    /// <param name="accreditation">The Accreditation object to persist.</param>
                    /// <returns>The Accreditation object potentially altered by the act of persisting it.</returns>
                    [Transactional(TransactionalTypes.TransactionScope)]
                    public Private.Accreditation Update(Private.Accreditation accreditation)
                    {
                        try
                        {

                            using (var manager = ContextManager<MUM_dev>.GetManager(Database.BusinessSystem))
                            {
                                // Set up some local variables to hold the return values from the stored procedure calls

                                Nullable<DateTime> dateTime = null;
                                Nullable<Guid> guid = null;

                                if (accreditation.IsNew)
                                {
                                    throw new NotImplementedException("The functionality to save a new Accrreditation has not yet been implemented");
                                }
                                else if (accreditation.IsDirty)
                                {
                                    // Set default time for anything happening next
                                    dateTime = DateTime.Now;

                                    Common.UserAccreditationList attempts = accreditation.Attempts;

                                    if (attempts.IsDirty)
                                    {
                                        // TODO find a cleaner way of inserting user accreditations, walking the list doesn't feel right!
                                        // Loop thorough the user accreditation list and save dirty one's
                                        foreach (var userAccreditation in attempts)
                                        {
                                            if (userAccreditation.IsNew)
                                            {
                                                Nullable<Guid> userGUID = userAccreditation.UserGUID;

                                                // Insert a new User Accreditation record
                                                manager.DataContext.UspUserAccreditationInsert(ref guid, userAccreditation.AccreditationGUID, userAccreditation.VersionID, userGUID, userAccreditation.Accredited, userGUID, ref dateTime);

                                                // Load the inserted values into the User Accreditation object
                                                LoadProperty(userAccreditation, Common.UserAccreditation.GUIDProperty, guid.Value);
                                                LoadProperty(userAccreditation, Common.UserAccreditation.InsertDateTimeProperty, dateTime.Value);
                                                LoadProperty(userAccreditation, Common.UserAccreditation.InsertUserGUIDProperty, userGUID.Value);
                                                LoadProperty(userAccreditation, Common.UserAccreditation.UpdateDateTimeProperty, dateTime.Value);
                                                LoadProperty(userAccreditation, Common.UserAccreditation.UpdateUserGUIDProperty, userGUID.Value);

                                                // Make sure that CSLA thinks this *hasn't* changed since being instantiated
                                                MarkOld(userAccreditation);
                                            }
                                        }
                                    }
                                }
                                // Make sure that CSLA knows this has changed since being instantiated
                                MarkOld(accreditation);

                                return accreditation;
                            }
                        }
                        catch (Exception ex)
                        {
                            mLog.Error(ex.ToString());
                            throw ex;
                        }
                    }

            #endregion

        #endregion

    }
}
